<!DOCTYPE html>
<html>
<head>
<title>用户管理</title>
<link href="../../../common/all.css" rel="stylesheet">
<script type="text/javascript" src="../../../common/all.js"></script>

<style type="text/css">

.form-actions{
     padding-right: 1000px;
}
#messageBox{
 color: red;
}
</style>


<script type="text/javascript">

//全部角色
var rolelist="";

//初始化
$(function(){
	rolelist=getRoles();
	
	initUpdate();
})


/**
*获取全部角色
*/
function getRoles()
{
	
	if(typeof(rolelist)!="undefined"&&rolelist!=""&&rolelist!=null&&rolelist.length>0)
	{
		return rolelist;
	}
	
	//获取角色
	$.ajax({
		url : getContextPath()+"/sys/role/listAll",
		type : "GET",
		//contentType: "application/json; charset=utf-8",
		dataType : "json",
		//data :{productVoJson:JSON.stringify(objVo)},
		data :'',
		success : function(returnData,status,XMLHttpRequest)
		{	
			if(resolveResultBeanIsError(returnData,status))
			{
				rolelist=returnData.data;
				return rolelist;
			}else{
				return "";
			}
				
		},
		error:function() {
			alert("请求角色异常！");
		}
	});
}
 



function initUpdate(){
	var id=getUrlParam("id");
	if(id==null||id==""){
		alert("参数异常，无法修改！");
		return;
	}
	$("#_userId").val(id);
	//请求数据
	$.ajax({
		url : getContextPath()+"/sys/user/get",
		type : "get",
		//contentType: "application/json; charset=utf-8",
		dataType : "json",
		data :{"id":id},
		success : function(returnData,status,XMLHttpRequest)
		{	
		   if(resolveResultBeanIsError(returnData,status))
		   {
			  var data=returnData.data;
			  $("#_userName").val(data.userName);
			  
			  $("#_fullName").val(data.fullName);
			  $("#_email").val(data.email);

			  //状态redio
			  var status=data.status;
			  if(status==0){
				  $("input[name=status]:eq(0)").attr("checked",'checked'); 
			  }else if(status==1)
			  {
				  $("input[name=status]:eq(1)").attr("checked",'checked'); 

			  }
			  
			  //角色类型
			//  $("#_type").attr("value",data.type);
			  $("#_type option[value='"+data.type+"']").attr("selected", "selected");  
			  //角色
		      var roles=data.roles;
		      if(typeof(rolelist)=="undefined"||rolelist==""||rolelist==null)
		      {
		    	//延迟 
		    	// var xRolelist=getRoles();
		    	  setTimeout(function () { 
		    		  initRoleCehcked(rolelist,data.roles);
		    	    }, 800);
			      
		      }else{
	    		  initRoleCehcked(rolelist,roles);
		      }
		      //角色 end
		      
		      	//字典
		        initDictByType("role_type","_type",data.type);
				
			  
			  $("#_remake").val(data.remake);
			   
		   }
		},
		error : function() {
			alert("请求异常！");
		}
	});
	
}

//初始用户拥有的角色
function initRoleCehcked(rolelist,roles){

    if(typeof(rolelist)!="undefined"&&rolelist!=""&&rolelist.length>0)
    {
	    	var roleHtml='';
			for(var i=0;i<rolelist.length;i++)
			{
				//db数据
				var roleObj=rolelist[i];
				roleHtml+=' <input value="'+roleObj.id+'"';
				
				if(roles!=""&&roles!=null&&roles.length>0)
				{
					for(var j=0;j<roles.length;j++)
					{
						//用户数据
						var dataId=roles[j].id;
						if(roleObj.id==dataId){
							roleHtml+=' checked="true"  ';
							break;
						}
					}
				}
				roleHtml+=' type="checkbox"  name="roles['+i+'].id"/>';
				// roleHtml+=' <input value="'+roleObj.id+'" type="checkbox"  name="roles['+i+'].id"/>';
				roleHtml+=roleObj.name;
				roleHtml+='&nbsp;&nbsp;';
			}
			$("#_role_plan_div").html(roleHtml);
     }
}


    //提交表单
	$(document).ready(function() {

		$("#inputForm").validate({
			submitHandler: function(form){
				updateUser();
				//form.submit();
			},
			errorContainer: "#messageBox",
			errorPlacement: function(error, element) {
				$("#messageBox").text("输入有误，请先更正。");
				if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
					error.appendTo(element.parent().parent());
				} else {
					error.insertAfter(element);
				}
			}
		});
	});
	
	
	function updateUser(){
		var jobj=$("#inputForm").serialize();
		$.ajax({
			url : getContextPath()+"/sys/user/update",
			type : "POST",
			dataType : "json",
			data :jobj,
			success : function(returnData,status,XMLHttpRequest)
			{	
			    resolveResultBeanIsOk(returnData,status);
			}
		});
		
	}
			

</script>

</head>

<body style="overflow-x: hidden; overflow-y: hidden;">
	
	<br/>
 <div class="userAddClz">
             
      <form id="inputForm" action="" class="form-horizontal" role="form">
    
              <input  name="id"  id="_userId" type="hidden" />
          
            <div class="form-group">
               <label class="col-sm-1 control-label" > <span class="help-inline"><font color="red">*</font> </span>登陆名：   
               </label>
               <div class="col-sd-1">
                  <input class="required input-xlarge" maxlength="20" name="userName"  id="_userName" type="text" />
               </div>
         
            </div>
            
            
            <div class="form-group">
               <label class="col-sm-1 control-label" >密码：       
               </label>
               <div class="col-sd-1">
                  <input  maxlength="16"  placeholder="不增不修改密码" name="password" type="password" id="_password"/>
               </div>
            </div>
            
            
            <div class="form-group">
               <label class="col-sm-1 control-label" >重新输入密码：       
               </label>
               <div class="col-sd-1">
                  <input  maxlength="16" placeholder="不增不修改密码"  name="repeatPassword" type="password" id="_repeatPassword"/>
               </div>
            </div>
            
            
            <div class="form-group">
               <label class="col-sm-1 control-label "> <span class="help-inline"><font color="red">*</font>姓名：</label>
               <div class="col-sd-1">
                  <input class="required" type="text"  maxlength="20"  name="fullName" id="_fullName"/>
               </div>
           
            </div>
            
            
              <div class="form-group">
               <label class="col-sm-1 control-label " >
               <span class="help-inline"><font color="red">*</font>状态：</label>
               <div class="col-sd-1">
			      &nbsp;  &nbsp;&nbsp;启用 <input type="radio" name="status" value="0" checked="checked"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;禁用<input type="radio" name="status" value="1"/>
               </div>
            </div>
            
            <div class="form-group">
               <label class="col-sm-1 control-label ">邮箱：</label>
               <div class="col-sd-1">
                  <input  type="text"  maxlength="30"  name="email" id="_email"/>
               </div>
            </div>
            
            <div class="form-group">
               <label class="col-sm-1 control-label ">用户类型：</label>
               <div class="col-sd-1">
                  
                  <select name="type" id="_type">
<!--                   <option value="1">系统管理员</option> -->
<!--                   <option value="2">商家管理</option> -->
<!--                   <option value="3">政府管理</option> -->
                  </select>
               </div>
            </div>
            
<!--           <div class="form-group"> -->
<!--                <label class="col-sm-1 control-label ">部门：</label> -->
<!--                <div class="col-sd-1"> -->
<!--                   <input value="1"  type="checkbox"  name="department.id"/> -->
<!--                </div> -->
<!--             </div> -->
            
             
            
             <div class="form-group">
               <label class="col-sm-1 control-label ">角色：</label>
               <div class="col-sd-1" id="_role_plan_div">
               </div>
            </div>
            
            
            <div class="form-group">
               <label class="col-sm-1 control-label " >
                                            备注：</label>
               <div class="col-sm-1">
               <textarea rows="6" cols="60" name="remake" id="_remake"></textarea>
               </div>
            </div>
            
         
            <div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		    </div>    
         </form>
         
    </div>
		


</body>
</html>